#ifndef NR_IO_H
#define NR_IO_H

#include "nr_data.h"
#include "nr_device_type.h"
#include "nr_device_item_server.h"
#include "nr_scheduler.h"
#include "nr_user.h"
#include "nr_role.h"

class nr_io : public nr_object
{
public:
    nr_io();

    ~nr_io();

    QString& host() { return m_host; }
    
    quint16& port() { return m_port; }

    QString& username() { return m_username; }
    
    QString& password() { return m_password; }

    virtual nr_error_t connect() = 0;

    virtual nr_error_t read_device_types(nr_device_types& device_types)  { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_device_types(nr_device_types& device_types) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t read_device_items(nr_device_item_server& item_server) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_device_items(nr_device_item_server& item_server) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t read_schedulers(QString& active_scheduler, nr_schedulers& schedulers) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_schedulers(const QString& active_scheduler, nr_schedulers& schedulers) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t read_roles(nr_roles &roles) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_roles(nr_roles &roles) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t read_users(nr_users &users) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_users(nr_users &users) { return NR_ERROR_NOT_IMPLEMENTED; }
    
    virtual nr_error_t write_data(nr_data* data) = 0;

    virtual nr_error_t write_event(nr_event* evt) = 0;

    virtual nr_error_t query_data(const nr_device_id_t& device_id, QDateTime &begin_time, 
        QDateTime &end_time, QList<nr_data*>& data_list) = 0;

    virtual nr_error_t query_event(const nr_device_id_t& device_id, QDateTime &begin_time, 
        QDateTime &end_time, QList<nr_event*>& data_list) = 0;

protected:
    QString m_host;
    quint16 m_port;
    QString m_username;
    QString m_password;
};

class nr_io_factory
{
public:
    typedef enum nr_io_type_enum
    {
        NR_IO_TYPE_UNKNOWN,
        NR_IO_TYPE_MONGODB,
        NR_IO_TYPE_MYSQL,
        NR_IO_TYPE_XML
    } nr_io_type_t;

    static nr_io* create(nr_io_type_t type);
    
    static void destroy(nr_io* io);

    static nr_io_factory::nr_io_type_t parse_IO_type(const QString &type_str );
};

#endif // NR_IO_H
